<template>
  <div class="pay-wx-set gray-box top-redius border-bottom-gray">
    <div class="title">
      {{ $t('sysManagement.wechatPaymentConfiguration') }}
    </div>
    <el-form
      ref="dataFormRef"
      label-width="180px"
      class="set-form"
      label-position="left"
      :rules="dataRule"
      :model="dataForm"
      @submit.prevent
      @keyup.enter="onSubmit()"
    >
      <el-form-item
        :label="`${$t('sysManagement.wechatPay')} mchId:`"
        style="width:640px"
        prop="mchId"
      >
        <el-input
          v-model="dataForm.mchId"
          :placeholder="`${$t('sysManagement.wechatPay')} mchId`"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="`${$t('sysManagement.paymentType')}:`"
        style="width:640px"
        prop="version"
      >
        <el-radio-group v-model="dataForm.version">
          <el-radio :label="2">
            v2
          </el-radio>
          <el-radio :label="3">
            v3
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- v2配置 -->
      <el-form-item
        v-if="dataForm.version === 2"
        :label="`${$t('sysManagement.wechatPay')} mchKey:`"
        style="width:640px"
        prop="mchKey"
      >
        <el-input
          v-model="dataForm.mchKey"
          :placeholder="`${$t('sysManagement.wechatPay')} mchKey`"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        v-if="dataForm.version === 2"
        :label="`${$t('sysManagement.paymentCertificatePath')}:`"
        style="width:640px"
        prop="keyPath"
      >
        <el-input
          v-model="dataForm.keyPath"
          :placeholder="`${$t('sysManagement.paymentCertificatePath')}`"
          controls-position="right"
        />
      </el-form-item>
      <!-- v3配置 -->
      <el-form-item
        v-if="dataForm.version === 3"
        :label="`${$t('sysManagement.wechatPay')} apiv3Key:`"
        style="width:640px"
        prop="apiv3Key"
      >
        <el-input
          v-model="dataForm.apiv3Key"
          :placeholder="`${$t('sysManagement.wechatPay')} apiv3Key`"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        v-if="dataForm.version === 3"
        :label="`${$t('sysManagement.certificateSerialNumber')}:`"
        style="width:640px"
        prop="certSerialNo"
      >
        <el-input
          v-model="dataForm.certSerialNo"
          :placeholder="`${$t('sysManagement.certificateSerialNumber')}`"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        v-if="dataForm.version === 3"
        :label="`${$t('sysManagement.merchant')}apiclient_key.pem:`"
        style="width:640px"
        prop="privateKeyContent"
      >
        <el-input
          v-model="dataForm.privateKeyContent"
          type="textarea"
          rows="10"
          :placeholder="`${$t('sysManagement.merchant')}apiclient_key.pem`"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        v-if="dataForm.version === 3"
        :label="`${$t('sysManagement.merchant')}apiclient_cert.pem:`"
        style="width:640px"
        prop="privateCertContent"
      >
        <el-input
          v-model="dataForm.privateCertContent"
          type="textarea"
          rows="10"
          :placeholder="`${$t('sysManagement.merchant')}apiclient_cert.pem`"
          controls-position="right"
        />
      </el-form-item>
      <div
        class="default-btn"
        @click="onSubmit()"
      >
        {{ $t('sysManagement.save') }}
      </div>
    </el-form>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'

const emit = defineEmits(['refreshDataList'])
const dataForm = reactive({
  mchId: null,
  mchKey: null,
  keyPath: null,
  signType: null,
  version: 3,
  apiv3Key: null,
  certSerialNo: null,
  privateKeyPath: null,
  privateKeyContent: null,
  privateCertPath: null,
  privateCertContent: null
})
const dataFormRef = ref(null)
const dataRule = {
  version: [
    { required: true, message: `${$t('sysManagement.paymentType')} ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ],
  mchId: [
    { required: true, message: `${$t('sysManagement.wechatPay')} mchId ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ],
  mchKey: [
    { required: true, message: `${$t('sysManagement.wechatPay')} mchKey ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ],
  keyPath: [
    { required: true, message: `${$t('sysManagement.paymentCertificatePath')} ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ],
  apiv3Key: [
    { required: true, message: `${$t('sysManagement.wechatPay')} apiv3Key ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ],
  certSerialNo: [
    { required: true, message: `${$t('sysManagement.certificateSerialNumber')} ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ],
  privateKeyContent: [
    { required: true, message: `${$t('sysManagement.merchant')}apiclient_key.pem ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ],
  privateCertContent: [
    { required: true, message: `${$t('sysManagement.merchant')}apiclient_cert.pem ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ]
}
onMounted(() => {
  init()
})

const init = () => {
  nextTick(() => {
    http({
      url: http.adornUrl('/sys/pconfig/info/' + 'WXPAY_CONFIG'),
      method: 'get',
      params: http.adornParams()
    }).then(({ data }) => {
      if (data) {
        Object.assign(dataForm, JSON.parse(data))
      }
    })
    dataFormRef.value?.resetFields()
  })
}
// 表单提交
const onSubmit = () => {
  dataFormRef.value?.validate((valid) => {
    if (valid) {
      http({
        url: http.adornUrl('/sys/pconfig/save'),
        method: 'post',
        data: http.adornData({
          paramKey: 'WXPAY_CONFIG',
          paramValue: JSON.stringify(dataForm),
          remark: $t('sysManagement.paymentCertificatePath')
        })
      }).then(() => {
        ElMessage({
          message: $t('publics.operation'),
          type: 'success',
          duration: 1500,
          onClose: () => {
            emit('refreshDataList')
          }
        })
      })
    }
  })
}
defineExpose({ init })
</script>
